package ve.com.wayoyo.wuml_case.DibujoDiagramas;

import java.io.File;
import java.util.ArrayList;

import ve.com.wayoyo.wuml_case.UserPref;
import ve.com.wayoyo.wuml_case.Exceptions.TituloVacioExecption;

import android.graphics.Canvas;
import android.graphics.Point;
import android.os.Environment;
import android.widget.Toast;

public class DiagramaClases extends Diagrama {

	
	private ArrayList<Componente> _components = new ArrayList<Componente>();
	private ArrayList<CompConector> _connectors =  new ArrayList<CompConector>();
	private int lastIdGiven = 0; 
	
	
	@Override
	public void AgregarComponente(Componente nuevo) {
		nuevo.setID(lastIdGiven + 1);
		lastIdGiven += 1;
		_components.add(nuevo);
	}

	@Override
	public void EliminarComponente(int id) {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public void exportarPNG() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void Dibujar(Canvas c,int offSetX,int offSetY, float GlobalScale) {
		for (Componente Value : _components) {
			//if(Value.CalcularVisibilidad(offSetX,offSetY,GlobalScale)){
				Value.Dibujar(c, false, GlobalScale,offSetX,offSetY);
			//}
		}
		
	}

	@Override
	public int checkOnTouch(Point touchPoint,String type) {
		CompConector aux = null;
		if(_SelectedConector != null){
			_SelectedConector.setState(CompConector.IDLE_STATE);
			_SelectedConector = null;
		}
		int respuesta = VistaDiagrama.IDLE;
		if(type.equals("Select")){
			for (Componente Value : _components) {
				if(Value.calcularTouch(touchPoint) == true){
					respuesta = VistaDiagrama.ITEM_SELECTED;
					SelectedItem = Value;
					SelectedItem.set_currentState(Componente.SELECTED);
					_SelectedConector = null;
					break;
				}else{
					if(aux == null){
						aux = Value.calcularTouchConectores(touchPoint);
					}
				}
			}
			if(aux != null){
				_SelectedConector = aux;
				respuesta = VistaDiagrama.CONNECTOR_SELECTED;
				_SelectedConector.setState(CompConector.SELECTED_STATE);
				getComponenteById(_SelectedConector.get_fatherId()).set_currentState(Componente.LINKED);
				getComponenteById(_SelectedConector.get_childId()).set_currentState(Componente.LINKED);
			}
		}
		if(type.equals("Connect")){
			for (Componente Value : _components) {
				if(Value.calcularTouch(touchPoint) == true){
					respuesta = VistaDiagrama.ITEM_SELECTED;
					CompConector nuevo = new CompConector(SelectedItem.ID, Value.ID);
					Value.actualizarAchorsPoints();
					Value.actualizarPuntoCentral();
					SelectedItem.actualizarAchorsPoints();
					SelectedItem.actualizarPuntoCentral();
					nuevo.CalcularConexiones(SelectedItem, Value);
					SelectedItem._conectorsBegining.add(nuevo);
					Value._conectorsEnd.add(nuevo);
					SelectedItem.set_currentState(Componente.SELECTED);
					break;
				}
			}	
		}
		return respuesta;
	}

	@Override
	public void resetFocus() {
		for (Componente Value : _components) {
			Value.removeFocus();
		}
		SelectedItem = null;
	}

	@Override
	public boolean validarNombre(String nuevo) throws TituloVacioExecption {
		boolean respuesta = true;
		if(nuevo.length() < 1){
			throw new TituloVacioExecption();
		}
		if(UserPref.getInstancia().isUsingConvections() == false){
			for (Componente Value : _components) {
				if(Value.getTitulo().equals(nuevo))
					respuesta = false;
			}
		}else{
			String nuevoModf = nuevo.substring(0,1).toUpperCase() + nuevo.substring(1);
			for (Componente Value : _components) {
				if(Value.getTitulo().equals(nuevoModf))
					respuesta = false;
			}
		}
		return respuesta;
	}
	
	@Override
	public void moverItemSeleccionado(int distX, int distY) {
		if(SelectedItem != null){
			SelectedItem.mover(distX, distY);
			SelectedItem.actualizarPuntoCentral();
			SelectedItem.actualizarAchorsPoints();
		}
	}

	public void agregarAtributo(CompAtributo comp)
	{
		((CompClase)SelectedItem).addAtributo(comp);
	}

	@Override
	public Componente getComponenteById(int id){
		for (Componente value : _components) {
			if(value.getID() == id)
				return value;
		}
		return null;
	}

	@Override
	public int getDiagramHeight() {
		int minValueY = Integer.MAX_VALUE;
		int maxValueY = Integer.MIN_VALUE;
		for (Componente value : _components) {
			if(value.getAnchor().y <= minValueY){
				minValueY = value.getAnchor().y;
			}
			if((value.getAnchor().y+value.get_height()) > maxValueY){
				maxValueY = (int) (value.getAnchor().y+value.get_height());
			}
		}
		return Math.abs(maxValueY-minValueY);
	}

	@Override
	public int getDiagramWidth() {
		int minValueX = Integer.MAX_VALUE;
		int maxValueX = Integer.MIN_VALUE;
		for (Componente value : _components) {
			if(value.getAnchor().x <= minValueX){
				minValueX = value.getAnchor().x;
			}
			if((value.getAnchor().x+value.get_height()) > maxValueX){
				maxValueX = (int) (value.getAnchor().x+value.get_width());
			}
		}
		return Math.abs(maxValueX-minValueX);
	}

	@Override
	public int getDiagramXoffSet() {
		int minValueX = Integer.MAX_VALUE;
		for (Componente value : _components) {
			if(value.getAnchor().x <= minValueX){
				minValueX = value.getAnchor().x;
			}
		}
		return -1*minValueX;
	}

	@Override
	public int getDiagramYoffSet() {
		int minValueY = Integer.MAX_VALUE;
		for (Componente value : _components) {
			if(value.getAnchor().y <= minValueY){
				minValueY = value.getAnchor().y;
			}
		}
		return -1*minValueY;
	}
	
	
}
